/*
SRMM

Copyright 2000-2005 Miranda ICQ/IM project, 
all portions of this codebase are copyrighted to the people 
listed in contributors.txt.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
#include "commonheaders.h"

struct GlobalMessageData *g_dat=NULL;
extern HINSTANCE g_hInst;
static HANDLE g_hDbEvent = 0, g_hAck = 0;
static int dbaddedevent(WPARAM wParam, LPARAM lParam);
static int ackevent(WPARAM wParam, LPARAM lParam);

void InitGlobals()
{
	g_dat = (struct GlobalMessageData *)malloc(sizeof(struct GlobalMessageData));
	g_dat->hMessageWindowList = (HANDLE) CallService(MS_UTILS_ALLOCWINDOWLIST, 0, 0);
	g_hDbEvent = HookEvent(ME_DB_EVENT_ADDED, dbaddedevent);
	g_hAck = HookEvent(ME_PROTO_ACK, ackevent);
	ReloadGlobals();
}

void FreeGlobals()
{
	if (g_dat)
		free(g_dat);

	if (g_hDbEvent) UnhookEvent(g_hDbEvent);
	if (g_hAck) UnhookEvent(g_hAck);
}

void ReloadGlobals() 
{
  g_dat->flags = 0;
  if (SRMMGetSettingByte(SRMSGSET_SHOWINFOLINE, SRMSGDEFSET_SHOWINFOLINE))
    g_dat->flags |= SMF_SHOWINFO;
  if (SRMMGetSettingByte(SRMSGSET_SHOWBUTTONLINE, SRMSGDEFSET_SHOWBUTTONLINE))
    g_dat->flags |= SMF_SHOWBTNS;
  if (SRMMGetSettingByte(SRMSGSET_SENDBUTTON, SRMSGDEFSET_SENDBUTTON))
    g_dat->flags |= SMF_SENDBTN;
  if (SRMMGetSettingByte(SRMSGSET_SHOWTYPING, SRMSGDEFSET_SHOWTYPING))
    g_dat->flags |= SMF_SHOWTYPING;
  if (SRMMGetSettingByte(SRMSGSET_SHOWTYPINGWIN, SRMSGDEFSET_SHOWTYPINGWIN))
    g_dat->flags |= SMF_SHOWTYPINGWIN;
  if (SRMMGetSettingByte(SRMSGSET_SHOWTYPINGNOWIN, SRMSGDEFSET_SHOWTYPINGNOWIN))
    g_dat->flags |= SMF_SHOWTYPINGTRAY;
  if (SRMMGetSettingByte(SRMSGSET_SHOWTYPINGCLIST, SRMSGDEFSET_SHOWTYPINGCLIST))
    g_dat->flags |= SMF_SHOWTYPINGCLIST;
  if (SRMMGetSettingByte(SRMSGSET_SHOWLOGICONS, SRMSGDEFSET_SHOWLOGICONS))
    g_dat->flags |= SMF_SHOWICONS;
  if (SRMMGetSettingByte(SRMSGSET_SHOWTIME, SRMSGDEFSET_SHOWTIME))
    g_dat->flags |= SMF_SHOWTIME;
  if (SRMMGetSettingByte(SRMSGSET_AVATARENABLE, SRMSGDEFSET_AVATARENABLE))
    g_dat->flags |= SMF_AVATAR;
  if (SRMMGetSettingByte(SRMSGSET_SHOWDATE, SRMSGDEFSET_SHOWDATE))
    g_dat->flags |= SMF_SHOWDATE;
  if (SRMMGetSettingByte(SRMSGSET_SHOWSECS, SRMSGDEFSET_SHOWSECS))
    g_dat->flags |= SMF_SHOWSECS;
  if (SRMMGetSettingByte(SRMSGSET_HIDENAMES, SRMSGDEFSET_HIDENAMES))
    g_dat->flags |= SMF_HIDENAMES;
  if (SRMMGetSettingByte(SRMSGSET_AUTOSAVEMSG, SRMSGDEFSET_AUTOSAVEMSG))
    g_dat->flags |= SMF_AUTOSAVEMSG;
  g_dat->openFlags = SRMMGetSettingDword(SRMSGSET_POPFLAGS, SRMSGDEFSET_POPFLAGS);
}

static int dbaddedevent(WPARAM wParam, LPARAM lParam)
{
	if (wParam) {
		HWND h = WindowList_Find(g_dat->hMessageWindowList, (HANDLE)wParam);
		if(h) SendMessage(h, HM_DBEVENTADDED, wParam, lParam);
	}
	return 0;
}

static int ackevent(WPARAM wParam, LPARAM lParam)
{
	ACKDATA *pAck = (ACKDATA *)lParam;
	
	if (!pAck) return 0;
	else if (pAck->type==ACKTYPE_AVATAR) {
		HWND h = WindowList_Find(g_dat->hMessageWindowList, (HANDLE)pAck->hContact);
		if(h) SendMessage(h, HM_AVATARACK, wParam, lParam);
	}
	else if (pAck->type==ACKTYPE_MESSAGE) {
		// need to broadcast due to multisend feature
		WindowList_Broadcast(g_dat->hMessageWindowList, HM_EVENTSENT, wParam, lParam);
	}
	return 0;
}
